package com.ruoyi.system.V2.font.service.impl;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.V2.font.mapper.QuotationMapper;
import com.ruoyi.system.V2.font.service.IQuotationService;
import com.ruoyi.system.model.domain.Backlist;
import com.ruoyi.system.model.domain.Quotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-28
 */
@Service
public class QuotationServiceImpl implements IQuotationService {
    @Autowired
    private QuotationMapper quotationMapper;


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Quotation selectQuotationById(Long id) {
        return quotationMapper.selectQuotationById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param quotation 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Backlist> selectQuotationList(Quotation quotation) {
        quotation.setCreateId(SecurityUtils.getUserId());
        List<Backlist> backlistList = quotationMapper.selectQuotationAndBack(quotation);

        return backlistList;
    }

    /**
     * @param quotation 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertQuotation(Quotation quotation) {
        Quotation select = new Quotation();
        select.setBackId(quotation.getBackId());
        select.setCreateId(SecurityUtils.getUserId());
        List<Quotation> quotations = quotationMapper.selectQuotationList(select);
        if (!CollectionUtils.isEmpty(quotations)) {
            throw new GlobalException("已提交报价");
        }
        quotation.setCreateTime(DateUtils.getNowDate());
        quotation.setCreateId(SecurityUtils.getUserId());
        return quotationMapper.insertQuotation(quotation);
    }

    /**
     * @return 结果
     */
    @Override
    public int updateQuotation(Quotation quotation) {
        quotation.setUpdateTime(DateUtils.getNowDate());
        quotation.setCreateId(SecurityUtils.getUserId());
        return quotationMapper.updateQuotation(quotation);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteQuotationByIds(Long[] ids) {
        return quotationMapper.deleteQuotationByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteQuotationById(Long id) {
        return quotationMapper.deleteQuotationById(id);
    }

    @Override
    public List<Quotation> mybprice(Quotation quotation) {

        List<Quotation> quotations = quotationMapper.selectQuotationList(quotation);
        for (Quotation quotation1 : quotations) {
            quotation1.setCreateName("报价人" + quotation1.getId());
        }

        return quotations;
    }
}
